﻿package com.tudou.home.model.ad
{
    import com.tudou.ad.consts.*;
    import com.tudou.ad.data.*;
    import com.tudou.ad.interfaces.*;
    import com.tudou.home.view.*;
    import com.tudou.track.*;
    import org.puremvc.as3.patterns.proxy.*;

    public class AdFrontMultiProxy extends Proxy
    {
        protected var _frontRemainDt:int = 0;
        protected var _adMaxPos:int = 0;
        protected var _adNow:int = 0;
        public static const NAME:String = "AdFrontMultiProxy";

        public function AdFrontMultiProxy()
        {
            super(NAME);
            return;
        }// end function

        public function reduceRemainDt(param1:int) : void
        {
            if (param1 > 0)
            {
                _frontRemainDt = _frontRemainDt - param1;
            }
            return;
        }// end function

        public function get frontRemainDt() : int
        {
            if (_frontRemainDt < 0)
            {
                _frontRemainDt = 0;
            }
            return _frontRemainDt;
        }// end function

        public function init() : void
        {
            var _loc_3:int = 0;
            var _loc_4:IAdItemInfo = null;
            _frontRemainDt = 0;
            _adMaxPos = 0;
            _adNow = 0;
            var _loc_1:* = facade.retrieveProxy(AdInfoProxy.NAME) as AdInfoProxy;
            var _loc_2:* = _loc_1.getFromerAds();
            if (_loc_2 == null)
            {
                return;
            }
            _adMaxPos = _loc_2.length;
            _loc_3 = 0;
            while (_loc_3 < _adMaxPos)
            {
                
                _loc_4 = _loc_2[_loc_3] as IAdItemInfo;
                if (_loc_4.durationType > 0)
                {
                    _frontRemainDt = _frontRemainDt + _loc_4.durationType;
                }
                if (AdType.PLACARD == _loc_4.type)
                {
                    AdConsts.PLACARD_DT = _loc_4.durationType;
                }
                _loc_3++;
            }
            return;
        }// end function

        public function skipAds() : void
        {
            var _loc_2:IAdItemInfo = null;
            var _loc_1:* = getNextAd();
            var _loc_3:int = 3;
            var _loc_4:int = 0;
            while (_loc_1 > 0 && _loc_4 < _loc_3)
            {
                
                _loc_4++;
                _loc_2 = AdData.getInst().getCurrAdItemByType(_loc_1);
                sendAop(NodeLog.AD_MULTI_NEW_SKIP, _loc_2);
                _loc_1 = getNextAd();
            }
            return;
        }// end function

        public function hasNextAd() : Boolean
        {
            if (_adNow == 0)
            {
                return false;
            }
            var _loc_1:* = facade.retrieveProxy(AdInfoProxy.NAME) as AdInfoProxy;
            var _loc_2:* = _loc_1.getCurrAdItemByType(_adNow);
            if (_adMaxPos > (_loc_2.orderPos + 1))
            {
                return true;
            }
            return false;
        }// end function

        public function get nowAd() : int
        {
            return _adNow;
        }// end function

        public function sendAop(param1:String, param2:IAdItemInfo) : void
        {
            var _loc_3:* = facade.retrieveProxy(AdProxy.NAME) as AdProxy;
            if (_loc_3.adTracker && param2)
            {
                _loc_3.adTracker.adSend(param2, AdData.getInst().adThrowInfo, param1, null);
            }
            return;
        }// end function

        public function getNextAd() : int
        {
            var _loc_1:* = AdData.getInst();
            var _loc_2:* = _loc_1.getAdItemByType(AdType.PLACARD);
            var _loc_3:* = facade.retrieveMediator(SkinMediator.NAME) as SkinMediator;
            _adNow = 0;
            if (_loc_2 != null)
            {
                if (!_loc_3.isFullScreen)
                {
                    _adNow = AdType.PLACARD;
                }
                else
                {
                    sendAop(NodeLog.AD_FULLSCREEN_SKIP, _loc_2);
                }
            }
            if (_adNow > 0)
            {
                return _adNow;
            }
            var _loc_4:* = _loc_1.getAdItemByType(AdType.FRONT);
            if (_loc_1.getAdItemByType(AdType.FRONT) != null)
            {
                if (AdConsts.PLACARD_DT > 0)
                {
                    reduceRemainDt(AdConsts.PLACARD_DT);
                    AdConsts.PLACARD_DT = 0;
                }
                _adNow = AdType.FRONT;
            }
            if (_adNow > 0)
            {
                return _adNow;
            }
            var _loc_5:* = _loc_1.getAdItemByType(AdType.FRONT_LAST);
            if (_loc_1.getAdItemByType(AdType.FRONT_LAST) != null)
            {
                if (AdConsts.PLACARD_DT > 0)
                {
                    reduceRemainDt(AdConsts.PLACARD_DT);
                    AdConsts.PLACARD_DT = 0;
                }
                _adNow = AdType.FRONT_LAST;
            }
            return _adNow;
        }// end function

    }
}
